/*
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.android.apps.mytracks.io.fusiontables;

import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.io.docs.SendDocsActivity;
import com.google.android.apps.mytracks.io.sendtogoogle.SendRequest;
import com.google.android.apps.mytracks.io.sendtogoogle.UploadResultActivity;

import android.accounts.Account;
import android.app.Instrumentation;
import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2;

/**
 * Tests the {@link SendFusionTablesActivity}.
 * 
 * @author Youtao Liu
 */
public class SendFusionTablesActivityTest extends
    ActivityInstrumentationTestCase2<SendFusionTablesActivity> {

  SendFusionTablesActivity sendFusionTablesActivity;
  Instrumentation instrumentation;
  SendRequest sendRequest;

  public SendFusionTablesActivityTest() {
    super(SendFusionTablesActivity.class);
  }

  /**
   * Creates a class to override some methods of
   * {@link SendFusionTablesActivity} to makes it testable.
   * 
   * @author youtaol
   */
  public class SendFusionTablesActivityMock extends SendFusionTablesActivity {
    /**
     * Ignores NullPointerException when new a intent in
     * {@link SendFusionTablesActivity#startNextActivity(boolean, boolean)} by
     * overriding this method.
     */
    @Override
    protected void startNextActivity(boolean success, boolean isCancel) {
      try {
        super.startNextActivity(success, isCancel);
      } catch (NullPointerException e) {}
    }

    /**
     * By overriding this method, avoids to start an activity.
     */
    @Override
    public void startActivity(Intent intent) {}

    public void setSendRequest(SendRequest sendRequest) {
      super.sendRequest = sendRequest;
    }
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    sendRequest = new SendRequest(1, true, false, true);
    sendRequest.setAccount(new Account("testAccountName", Constants.ACCOUNT_TYPE));
  }

  /**
   * Tests the method {@link SendFusionTablesActivity#CreateAsyncTask}.
   */
  public void testCreateAsyncTask() {
    Intent intent = new Intent();
    setActivityIntent(intent.putExtra(SendRequest.SEND_REQUEST_KEY, sendRequest));
    sendFusionTablesActivity = this.getActivity();
    instrumentation = getInstrumentation();
    SendFusionTablesAsyncTask sendFusionTablesAsyncTask = (SendFusionTablesAsyncTask) sendFusionTablesActivity
        .createAsyncTask();
    assertNotNull(sendFusionTablesAsyncTask);
  }

  /**
   * Tests the method {@link SendFusionTablesActivity#startNextActivity}. Sets
   * the values of two parameter to true and true. Sets the flag to send docs to
   * false.
   */
  public void testStartNextActivity_success_cancel_sendDocs() {
    SendFusionTablesActivityMock sendFusionTablesActivityMock = new SendFusionTablesActivityMock();
    sendRequest.setSendDocs(true);
    sendFusionTablesActivityMock.setSendRequest(sendRequest);
    sendFusionTablesActivityMock.startNextActivity(true, true);
    assertEquals(true, sendRequest.isFusionTablesSuccess());
    assertEquals(UploadResultActivity.class, sendFusionTablesActivityMock.next);
  }

  /**
   * Tests the method {@link SendFusionTablesActivity#startNextActivity}. Sets
   * the values of two parameter to true and false. Sets the flags to send docs
   * to false.
   */
  public void testStartNextActivity_success_notCancel_sendFusionTable() {
    SendFusionTablesActivityMock sendFusionTablesActivityMock = new SendFusionTablesActivityMock();
    sendRequest.setSendDocs(false);
    sendFusionTablesActivityMock.setSendRequest(sendRequest);
    sendFusionTablesActivityMock.startNextActivity(true, false);
    assertEquals(true, sendRequest.isFusionTablesSuccess());
    assertEquals(UploadResultActivity.class, sendFusionTablesActivityMock.next);
  }

  /**
   * Tests the method {@link SendFusionTablesActivity#startNextActivity}. Sets
   * the values of two parameter to false and false. Sets the flags to send docs
   * to true.
   */
  public void testStartNextActivity_failed_notCancel_sendNone() {
    SendFusionTablesActivityMock sendFusionTablesActivityMock = new SendFusionTablesActivityMock();
    sendRequest.setSendDocs(true);
    sendFusionTablesActivityMock.setSendRequest(sendRequest);
    sendFusionTablesActivityMock.startNextActivity(false, false);
    assertEquals(false, sendRequest.isFusionTablesSuccess());
    assertEquals(SendDocsActivity.class, sendFusionTablesActivityMock.next);
  }

}
